package com.example.sqliteopenhelper


import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.core.content.contentValuesOf
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val myDatabaseHelper = MyDatabaseHelper(this, "BookStore.db", 2)
        createDatabase.setOnClickListener {
            myDatabaseHelper.writableDatabase
        }
        //insert data
//        val db = myDatabaseHelper.writableDatabase
//        db.execSQL("insert into Book (name, author, pages, price) values(?, ?, ?, ?)",
//            arrayOf("The Da Vinci Code", "Dan Brown", "454", "16.96")
//        )
//        db.execSQL("insert into Book (name, author, pages, price) values(?, ?, ?, ?)",
//            arrayOf("The Lost Symbol", "Dan Brown", "510", "19.95")
//        )
//        val rawQuery = db.rawQuery("select *from Book", null)
//        Log.d("SQLiteOpenHelper", "${rawQuery.count}")

        queryData.setOnClickListener {
            val db = myDatabaseHelper.writableDatabase
            val rawQuery = db.rawQuery("select *from Book", null)
            if (rawQuery.moveToFirst()) {
                do {
                    val id = rawQuery.getInt(0)
                    val author = rawQuery.getString(1)
                    val price = rawQuery.getFloat(2)
                    val pages = rawQuery.getInt(3)
                    val name = rawQuery.getString(4)
                    Log.d(
                        "SQLiteOpenHelper",
                        "data{id=$id,author=$author,price=$price,pages=$pages,name=$name}"
                    )
                } while (rawQuery.moveToNext())
            }
        }

        replaceData.setOnClickListener {
            val db = myDatabaseHelper.writableDatabase
            db.beginTransaction()
            try {
                db.delete("Book", null, null)
//                if (true) {
//                    throw NullPointerException()
//                }

                //method one
//                val values = ContentValues().apply {
//                    put("name", "Game of Thrones")
//                    put("author", "George Martin")
//                    put("pages", 720)
//                    put("price", 20.85)
//                }
//                db.insert("Book", null, values)

                //method two
//                val cvOf = cvOf(
//                    "name" to "Game of Thrones",
//                    "author" to "George Martin",
//                    "pages" to 720,
//                    "price" to 20.85
//                )
//                db.insert("Book", null, cvOf)

                //method third
                val contentValuesOf = contentValuesOf(
                    "name" to "Game of Thrones",
                    "author" to "George Martin",
                    "pages" to 720,
                    "price" to 20.85
                )
                db.insert("Book", null, contentValuesOf)
                db.setTransactionSuccessful()
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                db.endTransaction()
            }
        }


        //add extension fun
//        getSharedPreferences("data", Context.MODE_PRIVATE).open {
//            putString("name", "Tom")
//            putInt("age", 28)
//            putBoolean("married", false)
//        }


        //androidx.core:core-ktx:1.0.2 extension package
//        getSharedPreferences("data", Context.MODE_PRIVATE).edit {
//            putString("name", "Tom")
//            putInt("age", 28)
//            putBoolean("married", false)
//        }
    }
}